Skip to main content

cov

cov(matrix1: any[] | Mat | Tensor, matrix2: any[] | Mat | Tensor) : number

param matrix1 an Array, Mat or Tensor, first half of the pair of data for finding the covariance.

param matrix2 an Array, Mat or Tensor, the other half of the pair of data for finding the covariance

returns - number - representing the covariance of A and B

To understand covariance, one must first understand variance. Variance of one variable, say matrix1, is a number that reflects how much matrix1 varies from its mean or average. That is, how much data fluctuates from the center/average of matrix1. The higher this is, the higher the variance is. It's called variance because the number being higher means more variance in the data, since there is more straying from the mean.

Covariance comes from variance but in the context of comparing two variables and their variance relative to each other. The best way to describe it is, the covariance is the number that reflects how highly (or inversely) two variables change, or vary together.

One thing to note is that another topic covered in this book, called, "correlation coefficient", is quite similar to covariance. The difference is that the normalized version of the covariance is the correlation coefficient.

This functions uses this formula:

$$cov(A, B) = \frac{1}{N - 1} \sum_{i=1}^{N}{(A_i - \overline{A})\cdot (B_i - \overline{B})}$$